using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using Home.Site.App_Start;
using Home.Site.Models.Account;
using Home.Site.Models.DataLayer;
using UserProfile = Home.Site.Models.Account.UserProfile;

namespace Home.Site.Controllers.Music
{
    public class MusicController : Controller
    {
        private readonly IAccountRepository accountRepository;
        private readonly IMusicAlbumsRepository musicAlbumsRepository;
        private readonly IMusicRepository musicRepository;

        public MusicController(IAccountRepository accountRepository, IMusicAlbumsRepository musicAlbumsRepository, IMusicRepository musicRepository)
        {
            this.accountRepository = accountRepository;
            this.musicAlbumsRepository = musicAlbumsRepository;
            this.musicRepository = musicRepository;
        }

        [Authorize]
        public ActionResult Create(IPrincipal currentUser)
        {
            var user = accountRepository.GetUser(currentUser);
            return View(user.IsAdmin ? "Create" : "Error");
        }

        [Authorize]
        [HttpPost]
        public ActionResult Create(IEnumerable<HttpPostedFileBase> fileUpload, Models.DataLayer.Music model, IPrincipal currentUser)
        {
            var user = accountRepository.GetUser(currentUser);
            if (!user.IsAdmin)
            {
                return View("Error");
            }
            foreach (var file in fileUpload)
            {
                if (file == null || file.ContentType != "audio/mp3") continue;
                var path = AppDomain.CurrentDomain.BaseDirectory + "MusicFiles\\" + user.UserName;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                var filename = Path.GetFileName(file.FileName);
                if (filename == null) continue;
                var playlist = musicAlbumsRepository.GetMusicAlbumsByUser(user.UserName).FirstOrDefault();
                if (playlist == null) continue;
                var playlistId = playlist.idplaylist;

                AddMusicToDb(filename, user, playlistId);
                var saveFileName = Path.Combine(path, filename);
                if (System.IO.File.Exists(saveFileName)) continue;
                file.SaveAs(saveFileName);
            }
            return RedirectToAction("UserProfile", "Account");
        }

        private void AddMusicToDb(string filename, UserProfile user, int playlist)
        {
            var isExist = musicRepository.FindByName(filename, user);
            if (isExist) return;
            var music = new Models.DataLayer.Music
                {
                    author = user.UserName,
                    filename = filename,
                    name = filename,
                    rate = 0,
                    playlist = playlist
                };
            musicRepository.InsertOrUpdate(music);
            musicRepository.Save();
        }
    }
}


